package ua.org.nuos.sdms.middle.service;

import ua.org.nuos.sdms.middle.entity.enums.Role;
import ua.org.nuos.sdms.middle.dao.UserDaoBean;
import ua.org.nuos.sdms.middle.entity.User;

import javax.annotation.Resource;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.PermitAll;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 13.11.11
 * Time: 16:09
 * To change this template use File | Settings | File Templates.
 */
@Stateless
@PermitAll
@DeclareRoles({Role.administrator, Role.client})
public class AuthenticationBean implements AuthenticationLocal {

    @Resource
    private SessionContext sctx;

    @EJB(beanName = "UserDaoBean")
    private UserDaoBean userDaoBean;

    @Override
    public boolean isUserInRole(String roleName) {
        return sctx.isCallerInRole(roleName);
    }

    @Override
    public User getCurrentUser() {
        String currentUserMail = sctx.getCallerPrincipal().getName();
        return userDaoBean.findByEmail(currentUserMail);
    }

}
